package com.bdqn.util;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class OrderDelayQueueConfig {


    public static final String ORDER_QUEUE_NAME = "order_queue";
    public static final String ORDER_EXCHANGE_NAME = "order_exchange";
    public static final String ORDER_DELAY_ROUTING_KEY = "order_delay_routing_key";

    public static final String DLX_ORDER_QUEUE_NAME = "dlx_order_queue";
    public static final String DLX_ORDER_EXCHANGE_NAME = "dlx_order_exchange";
    public static final String DLX_ORDER_DELAY_ROUTING_KEY = "dlx_order_delay_routing_key";

    //定义队列
    @Bean
    public Queue orderQueue(){
        Map<String, Object> args = new HashMap<>(16);
        args.put("x-dead-letter-exchange", DLX_ORDER_EXCHANGE_NAME);//配置死信交换机
        args.put("x-dead-letter-routing-key", DLX_ORDER_DELAY_ROUTING_KEY);
        return new Queue(ORDER_QUEUE_NAME, true, false, false, args);
    }

    //定义交换机
    @Bean
    public Exchange orderExchange(){
        return new DirectExchange(ORDER_EXCHANGE_NAME, true, false);
    }
    //定义绑定
    @Bean
    public Binding orderBinding(){
        //指定路由key，绑定队列和交换机
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDER_DELAY_ROUTING_KEY).noargs();
    }


    //定义死信队列，死信交换机
    @Bean
    public Queue dlxOrderQueue(){
        return new Queue(DLX_ORDER_QUEUE_NAME);
    }
    //定义交换机
    @Bean
    public Exchange dlxOrderExchange(){
        return new DirectExchange(DLX_ORDER_EXCHANGE_NAME, true, false);
    }
    //定义绑定
    @Bean
    public Binding dlxOrderBinding(){
        //指定路由key，绑定死信队列和死信交换机
        return BindingBuilder.bind(dlxOrderQueue()).to(dlxOrderExchange()).with(DLX_ORDER_DELAY_ROUTING_KEY).noargs();
    }
}
